﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class product_insurance_user_insurance_publicHospitalClaim : System.Web.UI.Page
{
    private ClaimBLL bll = new ClaimBLL();

    private const String FILE_PATH = "~/upload/insurance/";


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_claim_publicHospital_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        String docPath1 = fileUpload(fu_h1);
        String docPath2 = fileUpload(fu_h2);
        String docPath3 = fileUpload(fu_h3);
        String docPath4 = fileUpload(fu_h4);
        String docPath5 = fileUpload(fu_h5);
        String docPath6 = fileUpload(fu_h6);

        string userId = Session["userId"] as String;
        int insuredID = int.Parse(Request.QueryString["certificateNo"]);

        String insurance_acc = new AccountsManager().getInsuranceAccountNo(userId, "H");

        if (!Util.IsBlank(docPath1))
        {
            docPath1 = FILE_PATH + fileUpload(fu_h1);
        }
        if (!Util.IsBlank(docPath2))
        {
            docPath2 = FILE_PATH + fileUpload(fu_h2);
        }
        if (!Util.IsBlank(docPath3))
        {
            docPath3 = FILE_PATH + fileUpload(fu_h3);
        }
        if (!Util.IsBlank(docPath4))
        {
            docPath4 = FILE_PATH + fileUpload(fu_h4);
        }
        if (!Util.IsBlank(docPath5))
        {
            docPath5 = FILE_PATH + fileUpload(fu_h5);
        }
        if (!Util.IsBlank(docPath6))
        {
            docPath6 = FILE_PATH + fileUpload(fu_h6);
        }


        bll.newClaim(insurance_acc, "", "PENDING", 0, DateTime.Now, "", insuredID, "Reinbursement Claim Form", docPath1, "Inpatient Discharge Summary", docPath2, "Day Surgery Authorization Form", docPath3, "Refferal Letter", docPath4, "Final Summary Bill", docPath5, "Outpatient Bills", docPath6);
        this.Response.Redirect("./insurance_after_submit.aspx");
    }

    private String getFileNamePrefix()
    {
        string userId = Session["userId"] as String;
        int caseID = (new AccountsManager().getMaxValue("CLAIM", "caseID")) + 1;
        String fileName = userId + "_" + caseID + "_";


        return fileName;
    }



    private String fileUpload(FileUpload fileUpload)
    {
        string filename = "";
        if (fileUpload.HasFile)
        {
            try
            {
                filename = getFileNamePrefix() + fileUpload.FileName;
                fileUpload.SaveAs(Server.MapPath(FILE_PATH) + filename);

            }
            catch (Exception ex)
            {
                //TODO StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        return filename;
    }
    protected void vd_File_ServerValidate(object source, ServerValidateEventArgs args)
    {
   

        if (fu_h1.HasFile &&
            fu_h2.HasFile &&
            fu_h3.HasFile &&
            fu_h4.HasFile &&
            fu_h5.HasFile &&
            fu_h6.HasFile 
            )
        {
            args.IsValid = false;

        }
        else
        {
            args.IsValid = true;
        }
    }
}